//
//  editaddressViewController.m
//  lmq
//
//  Created by 千翼网络科技 on 2017/2/3.
//  Copyright © 2017年 yuanxin. All rights reserved.
//

#import "editaddressViewController.h"
#import "configddd.h"
#import <Masonry/Masonry.h>

#import "AddressPickerView.h"

@interface editaddressViewController ()<AddressPickerViewDelegate>

@property (nonatomic ,strong) AddressPickerView * pickerView;

@end

@implementation editaddressViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setTitle:@"修改收货地址"];
    self.view.backgroundColor=[ZJCModel colorWithString:@"f2f2f2"];
    
    NSLog(@"=====%@",_dataDic);
    [self.view addSubview:self.pickerView];
    
    UIButton *rightButton=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, screenScale(60), screenScale(30))];
    rightButton.titleLabel.font = get4sFont(14);
    [rightButton addTarget:self action:@selector(rightNavItemClick) forControlEvents:UIControlEventTouchUpInside];
    [rightButton setTitle:@"保存" forState:UIControlStateNormal];
    UIBarButtonItem *rightBarButton=[[UIBarButtonItem alloc]initWithCustomView:rightButton];
    self.navigationItem.rightBarButtonItem=rightBarButton;
    
    //收货人
    
    UIView *shrView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, DEF_SCREEN_WIDTH, screenScale(75))];
    shrView.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:shrView];
    
    UILabel *shrLabel=[[UILabel alloc]initWithFrame:CGRectMake(screenScale(20), 0, screenScale(130), screenScale(75))];
    shrLabel.text=@"收货人：";
    shrLabel.font = get4sFont(12);
    [shrView addSubview:shrLabel];
    
    shrTextField=[[UITextField alloc]initWithFrame:CGRectMake(screenScale(155), 0, screenScale(450), screenScale(75))];
    shrTextField.text = _dataDic[@"consignee"];
    shrTextField.font = get4sFont(12);
    [shrView addSubview:shrTextField];
    
    UIView *xianView1=[[UIView alloc]initWithFrame:CGRectMake(screenScale(20), screenScale(74), DEF_SCREEN_WIDTH-screenScale(20), screenScale(1))];
    xianView1.backgroundColor=[ZJCModel colorWithString:@"cccccc"];
    [shrView addSubview:xianView1];
    
    //手机号码
    
    UIView *phoneView=[[UIView alloc]initWithFrame:CGRectMake(0, screenScale(75), DEF_SCREEN_WIDTH, screenScale(75))];
    phoneView.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:phoneView];
    
    UILabel *phoneLabel=[[UILabel alloc]initWithFrame:CGRectMake(screenScale(20), 0, screenScale(130), screenScale(75))];
    phoneLabel.text=@"手机号码：";
    phoneLabel.font = get4sFont(12);
    [phoneView addSubview:phoneLabel];
    
    phoneTextField=[[UITextField alloc]initWithFrame:CGRectMake(screenScale(155), 0, screenScale(450), screenScale(75))];
    phoneTextField.text = _dataDic[@"phone"];
    phoneTextField.font = get4sFont(12);
    [phoneView addSubview:phoneTextField];
    
    UIView *xianView2=[[UIView alloc]initWithFrame:CGRectMake(screenScale(20), screenScale(74), DEF_SCREEN_WIDTH-screenScale(20), screenScale(1))];
    xianView2.backgroundColor=[ZJCModel colorWithString:@"cccccc"];
    [phoneView addSubview:xianView2];
    
    //所在地区
    
    UIView *diqvView=[[UIView alloc]initWithFrame:CGRectMake(0, screenScale(75)*2, DEF_SCREEN_WIDTH, screenScale(75))];
    diqvView.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:diqvView];
    
    UILabel *diqvLabel=[[UILabel alloc]initWithFrame:CGRectMake(screenScale(20), 0, screenScale(130), screenScale(75))];
    diqvLabel.text=@"所在地区：";
    diqvLabel.font = get4sFont(12);
    [diqvView addSubview:diqvLabel];
    
    [diqvView addSubview:self.addressBtn];
    
//    diqvTextField=[[UITextField alloc]initWithFrame:CGRectMake(screenScale(155), 0, screenScale(450), screenScale(75))];
//    diqvTextField.text = _dataDic[@"region"];
//    diqvTextField.font = get4sFont(12);
//    [diqvView addSubview:diqvTextField];
    
    UIView *xianView3=[[UIView alloc]initWithFrame:CGRectMake(screenScale(20), screenScale(74), DEF_SCREEN_WIDTH-screenScale(20), screenScale(1))];
    xianView3.backgroundColor=[ZJCModel colorWithString:@"cccccc"];
    [diqvView addSubview:xianView3];
    
    //详细地址
    
    UIView *detailView=[[UIView alloc]initWithFrame:CGRectMake(0, screenScale(75)*3, DEF_SCREEN_WIDTH, screenScale(150))];
    detailView.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:detailView];
    
    UILabel *detailLabel=[[UILabel alloc]initWithFrame:CGRectMake(screenScale(20), 0, screenScale(130), screenScale(75))];
    detailLabel.text=@"详细地址：";
    detailLabel.font =get4sFont(12);
    [detailView addSubview:detailLabel];
    
    detailTextField=[[UITextView alloc]initWithFrame:CGRectMake(screenScale(145), screenScale(25), screenScale(450), screenScale(125))];
    detailTextField.contentInset = UIEdgeInsetsMake(screenScale(-15), 0, 0, 0);
    [detailTextField setFont:get4sFont(12)];
    detailTextField.text = _dataDic[@"information"];
    [detailView addSubview:detailTextField];
    
    //  添加手势
    UITapGestureRecognizer *tapGesure = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(restoreKeyBoard)];
    [self.view addGestureRecognizer:tapGesure];
}

// 地址选择器

- (AddressPickerView *)pickerView{
    if (!_pickerView) {
        _pickerView = [[AddressPickerView alloc]initWithFrame:CGRectMake(0, scrHeight() , scrWidth(), screenScale(800))];
        _pickerView.delegate = self;
    }
    return _pickerView;
}

- (UIButton *)addressBtn{
    if (!_addressBtn) {
        _addressBtn = [[UIButton alloc]initWithFrame:CGRectMake(screenScale(155), 0, screenScale(450), screenScale(75))];
        _addressBtn.titleLabel.font = get4sFont(12);
        _addressBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        //        _addressBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
        [_addressBtn setTitle:_dataDic[@"region"] forState:UIControlStateNormal];
        //        [_addressBtn setTitle:@"收回地区选择器" forState:UIControlStateSelected];
        [_addressBtn setTitleColor:getColor(@"090909") forState:UIControlStateNormal];
        [_addressBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        //        _addressBtn.center = CGPointMake(self.addressLabel.center.x, self.addressLabel.center.y - 50);
    }
    return _addressBtn;
}

- (void)btnClick:(UIButton *)btn{
    btn.selected = !btn.selected;
    if (btn.selected) {
        [self.pickerView show];
    }else{
        [self.pickerView hide];
    }
}

#pragma mark - AddressPickerViewDelegate

- (void)cancelBtnClick{
    
    NSLog(@"点击了取消按钮");
    [self btnClick:_addressBtn];
}
- (void)sureBtnClickReturnProvince:(NSString *)province City:(NSString *)city Area:(NSString *)area{
    
    [_addressBtn setTitle:[NSString stringWithFormat:@"%@%@%@",province,city,area] forState:UIControlStateNormal];
    [self btnClick:_addressBtn];
}

#pragma mark 手势隐藏键盘
- (void)restoreKeyBoard
{
    [shrTextField resignFirstResponder];
    [phoneTextField resignFirstResponder];
    [diqvTextField resignFirstResponder];
    [detailTextField resignFirstResponder];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 点击右导航
-(void)rightNavItemClick{
    NSLog(@"保存");
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[[UIApplication sharedApplication]keyWindow] animated:YES];
    hud.tag = 500;
    //提示文本
    hud.labelText = @"正在加载...";
    //是否有遮罩
    hud.dimBackground = YES;
    
    NSString *uidStr=[PublicFunction getConfigWithKey:@"uid"];
    NSString *tokenStr=[PublicFunction getConfigWithKey:@"token"];
    NSMutableDictionary *productParams=[[NSMutableDictionary alloc]init];
    [productParams setValue:uidStr forKey:@"member_id"];
    [productParams setValue:tokenStr forKey:@"token"];
    [productParams setValue:shrTextField.text forKey:@"consignee"];
    [productParams setValue:phoneTextField.text forKey:@"phone"];
//    [productParams setValue:diqvTextField.text forKey:@"region"];
    [productParams setValue:_addressBtn.titleLabel.text forKey:@"region"];
    [productParams setValue:detailTextField.text forKey:@"information"];
    [productParams setValue:_dataDic[@"id"] forKey:@"id"];
    
    [[MNetworkHandler sharedInstance]conURL:Edit_Address
                                networkType:NetWorkPOST
                                     params:productParams
                                   delegate:nil
                                    showHUD:NO
                               successBlock:^(id returnData) {
                                   if ([[returnData objectForKey:@"code"]intValue]==1) {
                                       
                                       //[PublicFunction showAlert:nil message:[returnData objectForKey:@"msg"] duration:5.0f];
                                       NSLog(@"address===========%@",returnData);
                                       
                                       [hud removeFromSuperview];
                                       
                                       [self.navigationController popViewControllerAnimated:YES];
                                       
                                   }
                                   else{
                                       
                                       [hud removeFromSuperview];
                                       [PublicFunction showAlert:nil message:[returnData objectForKey:@"msg"] duration:5.0f];
                                       
                                   }
                               } failureBlock:^(NSError *error) {
                                   NSLog(@"%@",error);
                                   [hud removeFromSuperview];
                                   [PublicFunction showAlert:nil message:@"没有连接网络" duration:10.0f];
                               }];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
